<?php

class VacLogin extends DataObject implements PermissionProvider
{
    private static $db = array(
        "LoginToken" => "Varchar(50)"
    );

    private static $indexes = array(
        "LoginToken_ID" => array("type" => "index", "value" => "LoginToken, ID")
    );

    private static $has_one = array(
        "Resident" => "Resident"
    );

    private static $searchable_fields = array(
        "Resident.Username",
        "Resident.Email"
    );

    private static $summary_fields = array(
        "Resident.Username",
        "Resident.Email",
        "FormattedAccountStatus",
        "LoginToken",
        "Created.Nice24"
    );

    private static $default_sort = "ID DESC";

    private static $field_labels = array(
        "Resident.Username" => "Username",
        "Resident.Email" => "Email",
        "FormattedAccountStatus" => "Account Status",
        "Created.Nice24" => "Created At"
    );

    public function canView($member = null)
    {
        return Permission::check(array(
            "ADMIN",
            "VIEW_VacLogin"
        ), "any", $member);
    }

    public function canEdit($member = null)
    {
        return Permission::check(array(
            "ADMIN",
            "EDIT_VacLogin"
        ), "any", $member);
    }

    public function canDelete($member = null)
    {
        return Permission::check(array(
            "ADMIN",
            "DELETE_VacLogin"
        ), "any", $member);
    }

    public function canCreate($member = null)
    {
        return false;
    }

    public function FormattedAccountStatus()
    {
        switch ($this->Resident()->AccountStatus) {
            case Globals::STATUS_ACTIVE:
                return "Active";
            case Globals::STATUS_PENDING:
                return "Pending Approve";
            default:
                return "Deactivated";
        }
    }

    public function getCMSFields()
    {
        $fields = parent::getCMSFields();

        $usernameField = new LiteralField("Username", '<div class="field readonly text"><label class="left">Username</label><span class="readonly text"><a href="/admin/manageResident/Resident/EditForm/field/Resident/item/' . $this->ResidentID . '/edit" target="_blank">' . $this->Resident()->Username . '</a></span></div>');
        $fields->insertBefore($usernameField, "LoginToken");

        $createdDateField = new DatetimeField("Created", "Created At");
        $createdDateField->setConfig("datavalueformat", "MMM d, y HH:mm");
        $fields->addFieldToTab("Root.Main", $createdDateField);

        $modifiedDateField = new DatetimeField("LastEdited", "Last Modified At");
        $modifiedDateField->setConfig("datavalueformat", "MMM d, y HH:mm");
        $fields->addFieldToTab("Root.Main", $modifiedDateField);

        $fields->makeFieldReadonly("LoginToken");
        $fields->makeFieldReadonly("Created");
        $fields->makeFieldReadonly("LastEdited");

        $accountStatus = new DropdownField
        (
            "AccountStatus",
            _t("General.ACCOUNT_STATUS", "Account Status"),
            array(
                "active" => "Active",
                "pending" => "Pending Approve",
                "deactivated" => "Deactivated"
            ),
            $this->Resident()->AccountStatus
        );

        $fields->insertAfter($accountStatus, "Username");

        $vacTagList = VacTag::get()->filter(array("ResidentID" => $this->ResidentID))->sort("ID DESC");
        $vacTagHistoryField = new GridField("VacTagHistory", "Requested Code History", $vacTagList);
        $vacTagHistoryFieldColumns = $vacTagHistoryField->getConfig()->getComponentByType("GridFieldDataColumns");
        $vacTagHistoryFieldColumns->setDisplayFields(array(
            "TagCode" => "Code",
            "VehicleNo" => "Referred Vehicle No.",
            "FormattedTagStatus" => "Code Status",
            "Created.Nice24" => "Requested At",
            "Expires.Nice24" => "Expired At",
            "''" => ""
        ));

        $fields->addFieldToTab("Root.Main", $vacTagHistoryField);

        $fields->removeByName("ResidentID");

        return $fields;
    }

    protected function onAfterWrite()
    {
        $member = Member::get()->byID($this->ResidentID);

        if ($this->AccountStatus != $member->AccountStatus) {
            $member->AccountStatus = $this->AccountStatus;
            $member->write();
        }

        parent::onAfterWrite();
    }

    public function providePermissions()
    {
        $category = "Manage VAC login permissions";

        return array(
            "VIEW_VacLogin" => array(
                "name" => "View all records",
                "category" => $category
            ),
            "EDIT_VacLogin" => array(
                "name" => "Edit record",
                "category" => $category
            ),
            "DELETE_VacLogin" => array(
                "name" => "Delete record",
                "category" => $category
            )
        );
    }
}